package com.crack.test2;

import android.text.SpannableStringBuilder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.crack.spansframe.ClickMovement;
import com.crack.spansframe.SpanSolve;
import com.crack.test.R;

import java.util.List;

/**
 * Created by archermind on 16-4-18.
 */
public class SpanAdapter extends BaseAdapter {
    ListView mListView;
    List<SpannableStringBuilder> mSpanContents;
    LayoutInflater mInflater;

    public SpanAdapter(ListView listView, List<SpannableStringBuilder> spanContent) {
        mListView = listView;
        mSpanContents = spanContent;
        mInflater = LayoutInflater.from(listView.getContext());
    }

    @Override
    public int getCount() {
        return mSpanContents.size();
    }

    @Override
    public SpannableStringBuilder getItem(int position) {
        return mSpanContents.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        TextView textView;
        if (view == null) {
            view = mInflater.inflate(R.layout.span_list_item, parent, false);
        }
        textView = (TextView) view;
        textView.setMovementMethod(ClickMovement.getInstance());
        textView.setTag(position);
        SpannableStringBuilder sp = getItem(position);
        SpanSolve.injectTextView(sp, textView);
        textView.setText(sp);
        return view;
    }
}
